from collections import Counter
import matplotlib.pyplot as plt

# 读取文件
with open('Harry Potter.txt', 'r', encoding='utf-8') as file:
    text = file.read().lower()  # 转换为小写

# 替换标点符号为空格
chars_to_remove = ',;./?\"-~`!%&@(){}[]:<>'
translation_table = str.maketrans(chars_to_remove, ' ' * len(chars_to_remove))
text = text.translate(translation_table).replace("'"," ")
# 分词
words = text.split()

# 读取停用词
with open('stopwords.txt', 'r', encoding='utf-8') as sw:
    stopwords = set(sw.read().split())

# 过滤停用词和单字符单词
filtered_words = [word for word in words if word not in stopwords and len(word) >= 2]
# with open('words_Harry_Potter.txt', 'w', encoding='utf-8') as output_file:
#     for word in words:
#         output_file.write(word + '\n')

# 统计词频
word_counts = Counter(filtered_words)
# with open('word_counts.txt', 'w', encoding='utf-8') as word_counts_file:
#     for word, count in word_counts.items():
#         word_counts_file.write(f'{word} {count}\n')

# 获取前20个最频繁的单词
top_20_words = word_counts.most_common(20)

# 写入结果文件
with open('result.txt', 'w', encoding='utf-8') as result_file:
    for word, count in top_20_words:
        result_file.write(f'{word} {count}\n')

# 绘图
words, counts = zip(*top_20_words)
plt.figure(figsize=(10, 8))
plt.bar(words, counts)
plt.xlabel('Words')
plt.ylabel('Frequency')
plt.title('Top 20 Most Frequent Words in Harry Potter')
plt.show()
